#include "Wall.h"
#include <GL\glew.h>
#include <GL\glut.h>

Wall::Wall() : angle(0.0f), texId(0.0f)
{
}

Wall::Wall(float positionX,float positionY,float positionZ,
		   float scaleX,float scaleY, float scaleZ,
		   float red,float green,float blue,
		   float _angle, unsigned int _texId) : angle(_angle), texId(_texId)
{
	setPosition(positionX,positionY,positionZ);
	setScale(scaleX,scaleY,scaleZ);
	setColor(red,green,blue);
}

Wall::Wall(Vector3f& Position, Vector3f& Scale, Vector3f& Color, float _angle, unsigned int _texId) : angle(_angle), texId(_texId)
{
	setPosition(Position);
	setScale(Scale);
	setColor(Color);
}

void Wall::Draw(bool floor)
{
	glPushMatrix();
	glColor3f(color[0],color[1],color[2]);
	glTranslatef(position[0],position[1],position[2]);
	glScalef(scale[0],scale[1],scale[2]);

	// Angle = 0 BUG()
	//glRotatef(1.0,0,0,angle);
	
	if(texId != -1)
	{
		glEnable(GL_TEXTURE_2D);
		glBindTexture(GL_TEXTURE_2D, texId);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	}
	
	unsigned int textureCoeffV;
	unsigned int textureCoeffH;

	if(floor)
	{
		textureCoeffV = 10;
		textureCoeffH = 15;
	}else 
	{
		textureCoeffV = 4;
		textureCoeffH = 2;
	}

	glBegin(GL_QUADS);	
	
	//Top Face
	glNormal3f(0.0, 1.0f, 0.0f);
	glTexCoord2f(0.0f, 0.0f);
	glVertex3f(-UNIT_SIZE / 2, UNIT_SIZE / 2, UNIT_SIZE / 2);
	glTexCoord2f(textureCoeffV * 1.0f, 0.0f);
	glVertex3f(UNIT_SIZE / 2, UNIT_SIZE / 2, UNIT_SIZE / 2);
	glTexCoord2f(textureCoeffV * 1.0f, textureCoeffH * 1.0f);
	glVertex3f(UNIT_SIZE / 2, UNIT_SIZE / 2, -UNIT_SIZE / 2);
	glTexCoord2f(0.0f,textureCoeffH * 1.0f);
	glVertex3f(-UNIT_SIZE / 2, UNIT_SIZE / 2, -UNIT_SIZE / 2);	

	//Bottom face
	glNormal3f(0.0, -1.0f, 0.0f);
	glTexCoord2f(0.0f, 0.0f);
	glVertex3f(-UNIT_SIZE / 2, -UNIT_SIZE / 2, -UNIT_SIZE / 2);
	glTexCoord2f(textureCoeffV * 1.0f, 0.0f);
	glVertex3f(UNIT_SIZE / 2, -UNIT_SIZE / 2, -UNIT_SIZE / 2);
	glTexCoord2f(textureCoeffV * 1.0f, textureCoeffH * 1.0f);
	glVertex3f(UNIT_SIZE / 2, -UNIT_SIZE / 2, UNIT_SIZE / 2);
	glTexCoord2f(0.0f, textureCoeffH * 1.0f);
	glVertex3f(-UNIT_SIZE / 2, -UNIT_SIZE / 2, UNIT_SIZE / 2);

	//Left face
	glNormal3f(-1.0, 0.0f, 0.0f);
	glTexCoord2f(0.0f, 0.0f);
	glVertex3f(-UNIT_SIZE / 2, -UNIT_SIZE / 2, -UNIT_SIZE / 2);
	glTexCoord2f(0.0f,textureCoeffH *  1.0f);
	glVertex3f(-UNIT_SIZE / 2, -UNIT_SIZE / 2, UNIT_SIZE / 2);
	glTexCoord2f(textureCoeffV * 1.0f, textureCoeffH * 1.0f);
	glVertex3f(-UNIT_SIZE / 2, UNIT_SIZE / 2, UNIT_SIZE / 2);
	glTexCoord2f(textureCoeffV * 1.0f, 0.0f);
	glVertex3f(-UNIT_SIZE / 2, UNIT_SIZE / 2, -UNIT_SIZE / 2);	

	//Right face
	glNormal3f(1.0, 0.0f, 0.0f);
	glTexCoord2f(0.0f, 0.0f);
	glVertex3f(UNIT_SIZE / 2, -UNIT_SIZE / 2, UNIT_SIZE / 2);
	glTexCoord2f(0.0f, textureCoeffH * 1.0f);
	glVertex3f(UNIT_SIZE / 2, -UNIT_SIZE / 2, -UNIT_SIZE / 2);
	glTexCoord2f(textureCoeffV * 1.0f,textureCoeffH *  1.0f);
	glVertex3f(UNIT_SIZE / 2, UNIT_SIZE / 2, -UNIT_SIZE / 2);
	glTexCoord2f(textureCoeffV * 1.0f, 0.0f);
	glVertex3f(UNIT_SIZE / 2, UNIT_SIZE / 2, UNIT_SIZE / 2);
	
	//Front face
	glNormal3f(0.0, 0.0f, 1.0f);	
	glTexCoord2f(0.0f, 0.0f);
	glVertex3f(-UNIT_SIZE / 2, -UNIT_SIZE / 2, UNIT_SIZE / 2);
	glTexCoord2f(0.0f, textureCoeffH * 1.0f);
	glVertex3f(UNIT_SIZE / 2, -UNIT_SIZE / 2, UNIT_SIZE / 2);
	glTexCoord2f(textureCoeffV * 1.0f, textureCoeffH * 1.0f);
	glVertex3f(UNIT_SIZE / 2, UNIT_SIZE / 2, UNIT_SIZE / 2);
	glTexCoord2f(textureCoeffV * 1.0f, 0.0f);
	glVertex3f(-UNIT_SIZE / 2, UNIT_SIZE / 2, UNIT_SIZE / 2);

	//Back face
	glNormal3f(0.0, 0.0f, -1.0f);
	glTexCoord2f(0.0f, 0.0f);
	glVertex3f(-UNIT_SIZE / 2, -UNIT_SIZE / 2, -UNIT_SIZE / 2);
	glTexCoord2f(textureCoeffV * 1.0f, 0.0f);
	glVertex3f(-UNIT_SIZE / 2, UNIT_SIZE / 2, -UNIT_SIZE / 2);
	glTexCoord2f(textureCoeffV * 1.0f,textureCoeffH *  1.0f);
	glVertex3f(UNIT_SIZE / 2, UNIT_SIZE / 2, -UNIT_SIZE / 2);
	glTexCoord2f(0.0f, textureCoeffH * 1.0f);
	glVertex3f(UNIT_SIZE / 2, -UNIT_SIZE / 2, -UNIT_SIZE / 2);

	glEnd();

	if(texId != -1)
	{
		glDisable(GL_TEXTURE_2D);
	}

	glPopMatrix();
};

void Wall::Rotate(float _angle)
{
	angle += _angle;
}

